package com.wei.czz.framework.annex.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wei.czz.common.enums.CommonEnum;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wei.czz.framework.annex.dao.BusinessAnnexDao;
import com.wei.czz.framework.annex.entity.BusinessAnnexEntity;
import com.wei.czz.framework.annex.service.BusinessAnnexService;
import org.springframework.transaction.annotation.Transactional;


@Service("businessAnnexService")
@AllArgsConstructor
public class BusinessAnnexServiceImpl extends ServiceImpl<BusinessAnnexDao, BusinessAnnexEntity>
        implements BusinessAnnexService {

    private static final Logger log = LoggerFactory.getLogger(BusinessAnnexServiceImpl.class);

    @Transactional
    @Override
    public void batchSaveBusinessAnnex(List<BusinessAnnexEntity> businessAnnexList) {
        boolean bool = this.saveBatch(businessAnnexList);
        log.info("批量保存业务已关联附件配置完成。bool={}", bool);
    }

    @Override
    public List<BusinessAnnexEntity> getBusinessAnnexList(Long relationId, String relationCode, Integer classify) {
        if (Objects.isNull(relationId) && StringUtils.isBlank(relationCode)) {
            log.error("获取业务已关联附件配置参数错误。classify={}", classify);
        }
        LambdaQueryWrapper<BusinessAnnexEntity> businessAnnexLambdaQueryWrapper = new LambdaQueryWrapper<>();
        businessAnnexLambdaQueryWrapper.eq(Objects.nonNull(relationCode), BusinessAnnexEntity::getRelationId,
                relationCode)
                .eq(StringUtils.isNotBlank(relationCode), BusinessAnnexEntity::getRelationCode, relationCode)
                .eq(BusinessAnnexEntity::getClassify, classify)
                .eq(BusinessAnnexEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<BusinessAnnexEntity> businessAnnexList = baseMapper.selectList(businessAnnexLambdaQueryWrapper);
        log.info("查询业务已关联附件配置完成。relationId={} relationCode={} preId={} isEmpty={}", relationId,
                relationCode, classify, businessAnnexList.isEmpty());
        return businessAnnexList;
    }

    @Transactional
    @Override
    public void batchUpdateBusinessAnnex(List<BusinessAnnexEntity> businessAnnexList) {
        boolean bool = this.updateBatchById(businessAnnexList);
        log.info("批量保存业务已关联附件配置完成。bool={}", bool);
    }

}